package com.example.rental_system.controller;

import cn.hutool.core.util.PageUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.rental_system.pojo.House;
import com.example.rental_system.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class MainPage {
    @Autowired
    private HouseService houseService;
    @RequestMapping("/mainPage/{page}")
    public String mainPage( Model model,@PathVariable Integer page){
        IPage<House> houseIPage=houseService.queryByUserId(0,page,3);
        int[] rainbow = PageUtil.rainbow(page, (int)houseIPage.getPages(), 3);

        List<House> pageInfo = houseIPage.getRecords();
        int pageNo = (int)houseIPage.getCurrent();
        int totalPages = (int)houseIPage.getPages();
        model.addAttribute("pageInfo",pageInfo);//页面信息
        model.addAttribute("pageNo",pageNo);//当前页
        model.addAttribute("totalPages",totalPages);//总页数
        model.addAttribute("rainbow",rainbow);
        return "main";
    }
    @RequestMapping("/breakAdmin")
    public String breakAdmin(HttpSession session){
        session.removeAttribute("admin");
        session.removeAttribute("adminName");
        return "redirect:/";

    }
    @RequestMapping("/breakUser")
    public String breakUser(HttpSession session){
        session.removeAttribute("user");
        session.removeAttribute("userName");
        return "redirect:/";

    }
}
